package com.kvn.doc.export;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import com.kvn.doc.model.FieldSignature;
import com.kvn.doc.model.MvcMethodSignature;

/**
 * @author wzy
 * @date 2017年11月7日 上午11:00:14
 */
public class ConsoleExporter implements Exporter {

	private ConsoleExporter() {
		super();
	}

	private static class Holder {
		private static ConsoleExporter singleton = new ConsoleExporter();
	}

	public static ConsoleExporter getInstance() {
		return Holder.singleton;
	}

	@Override
	public void export(List<MvcMethodSignature> content, OutputStream os) {
		try {
			StringBuffer sb = new StringBuffer();
			for (MvcMethodSignature method : content) {
				sb.append("【功能】：\n");
				sb.append(method.getFunctionDesc()).append("\n");
				sb.append("【请求路径】：\n");
				sb.append(method.getAccessPath()).append("\n");
				sb.append("\n【参数说明】：\n");
				sb.append("参数|必选|类型|说明\n");
				for (FieldSignature param : method.getParamDescLs()) {
					sb.append(param.getFieldName()).append("|").append(param.getRequired()).append("|").append(param.getFieldType()).append("|")
							.append(param.getDesc()).append("\n");
				}
				sb.append("\n【返回字段说明】：\n");
				sb.append("字段|类型|说明\n");
				for (FieldSignature param : method.getReturnDescLs()) {
					sb.append(param.getFieldName()).append("|").append(param.getFieldType()).append("|").append(param.getDesc()).append("\n");
				}
			}
			
			
			os.write(sb.toString().getBytes());
			os.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}

}
